package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.domain.Account;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.account.client.VenueClient;
import com.woniu.account.client.model.VenueCertify;
import com.woniu.account.dao.VenueAdminRoleDao;
import com.woniu.account.dto.venueAdmin.VenueAdminDto;
import com.woniu.account.model.VenueAdmin;
import com.woniu.account.dao.VenueAdminDao;
import com.woniu.account.model.VenueAdminRole;
import com.woniu.account.param.venueAccount.AddVenueAdminParam;
import com.woniu.account.param.venueAccount.VenueAdminLoadParam;
import com.woniu.account.param.venueAccount.VenueAdminLoginParam;
import com.woniu.account.service.VenueAdminRoleService;
import com.woniu.account.service.VenueAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.exception.YuJiaException;
import com.woniu.exception.venueAdmin.VenueAdminException;
import com.woniu.exception.venueAdmin.VenueAdminExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.MD5Util;
import com.woniu.util.StateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月11日
 */
@Service
public class VenueAdminServiceImpl extends ServiceImpl<VenueAdminDao, VenueAdmin> implements VenueAdminService {
    @Autowired
    private VenueAdminDao venueAdminDao;
    @Autowired
    private VenueAdminRoleService venueAdminRoleService;
    @Autowired
    private MD5Util md5Util;
    @Autowired
    private VenueClient venueClient;

    @Override
    public VenueAdminDto login(VenueAdminLoginParam param) {
        QueryWrapper<VenueAdmin> qw = new QueryWrapper<VenueAdmin>().eq("venue_admin_phone", param.getVenueAccountName())
                .or().eq("venue_admin_name", param.getVenueAccountName())
                .or().eq("venue_admin_nickname", param.getVenueAccountName());
        VenueAdmin venueAdmin = venueAdminDao.selectOne(qw);
        if (ObjectUtil.isEmpty(venueAdmin))
            throw new VenueAdminException(VenueAdminExceptionCode.VENUE_ADMIN_NOT_EXIST);
        if (venueAdmin.getVenueAdminState() != StateUtil.VENUE_ADMIN_STATE_NORMAL)
            throw new VenueAdminException(VenueAdminExceptionCode.ACCOUNT_ADMIN_ABNORMAL);
        if (!md5Util.encryption(venueAdmin.getVenueAdminPhone(), param.getVenueAccountPass()).equals(venueAdmin.getVenueAdminPassword()))
            throw new VenueAdminException(VenueAdminExceptionCode.VENUE_ADMIN_ERROR);
        VenueAdminDto venueAdminDto = BeanUtil.toBean(venueAdmin, VenueAdminDto.class);
        Result<VenueCertify> venueCertifyByVenueId = venueClient.getVenueCertifyByVenueId(venueAdminDto.getVenueId());
        if (venueCertifyByVenueId.getCode()!=200)
            throw new YuJiaException(venueCertifyByVenueId.getCode(), venueCertifyByVenueId.getMsg());
        if(ObjectUtil.isNotEmpty(venueCertifyByVenueId.getData())){
        if (venueCertifyByVenueId.getData().getCertifyState() == 1) {
            throw new YuJiaException(500, "场馆入驻申请正在审核中，请稍等");
        }}
        return venueAdminDto;
    }

    @Override
    public PageInfo<VenueAdminDto> load(VenueAdminLoadParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        ArrayList<VenueAdminDto> venueAdminDtos = venueAdminDao.load(param);
        PageInfo<VenueAdminDto> venueAdminDtoPageInfo = new PageInfo<>(venueAdminDtos);
        return venueAdminDtoPageInfo;


    }

    @Override
    public void changeState(Integer venueAdminId, Integer state) {
        VenueAdmin venueAdmin = venueAdminDao.selectById(venueAdminId);
        if (ObjectUtil.isEmpty(venueAdmin))
            throw new VenueAdminException(VenueAdminExceptionCode.VENUE_ADMIN_NOT_EXIST);
        if (venueAdmin.getVenueAdminState() == state || venueAdmin.getVenueAdminState() == StateUtil.VENUE_ADMIN_STATE_DELETE)
            throw new VenueAdminException(VenueAdminExceptionCode.ACCOUNT_ADMIN_ABNORMAL);
        venueAdmin.setVenueAdminState(state);
        venueAdminDao.updateById(venueAdmin);
    }

    @Override
    public VenueAdminDto addVenueAdmin(AddVenueAdminParam param) {
//验证账号名是否重复
        QueryWrapper<VenueAdmin> qw = new QueryWrapper<VenueAdmin>();
        qw.eq("venue_admin_phone", param.getVenueAdminPhone());
        VenueAdmin venueAdmin = venueAdminDao.selectOne(qw);

        if (ObjectUtil.isNotEmpty(venueAdmin))
            throw new VenueAdminException(VenueAdminExceptionCode.VENUE_ADMIN_PHONE_HAD_EXIST);
        qw.eq("venue_admin_nickname", param.getVenueAdminNickname());
        VenueAdmin venueAdmin1 = venueAdminDao.selectOne(qw);
        if (ObjectUtil.isNotEmpty(venueAdmin1))
            throw new VenueAdminException(VenueAdminExceptionCode.VENUE_ADMIN_NICKNAME_HAD_EXIST);
        //新增
        //实现添加管理员
        VenueAdmin venueAdmin2 = BeanUtil.toBean(param, VenueAdmin.class);
        venueAdmin2.setVenueAdminPassword(md5Util.encryption(venueAdmin2.getVenueAdminPhone(), "123456"));
        venueAdminDao.insert(venueAdmin2);
        //实现添加管理员关联
        HashSet<Integer> integers = new HashSet<>(param.getVenueAdminRole());
        ArrayList<VenueAdminRole> venueAdminRoles = new ArrayList<>();
        for (Integer integer : integers) {
            VenueAdminRole venueAdminRole = new VenueAdminRole();
            venueAdminRole.setVenueAdminId(venueAdmin2.getVenueAdminId());
            venueAdminRole.setRoleId(integer);
            venueAdminRoles.add(venueAdminRole);
        }
        venueAdminRoleService.saveBatch(venueAdminRoles);
        VenueAdminDto venueAdminDto = BeanUtil.toBean(venueAdmin2, VenueAdminDto.class);
        return venueAdminDto;
    }

    @Override
    public List<VenueAdminDto> getVenueCoachsById(Integer roleId, Integer venueId) {
        return venueAdminDao.getVenueCoachsById(roleId, venueId);
    }

    @Override
    public List<VenueAdminDto> getByIds(Set<Integer> ids) {
        List<VenueAdminDto> venueAdminDtos = venueAdminDao.getByIds(ids);
        return venueAdminDtos;
    }
}
